package com.hzwq.demo2.role.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzwq.demo2.permission.service.IPermissionService;
import com.hzwq.demo2.role.entity.Role;
import com.hzwq.demo2.role.service.IRoleService;
import com.hzwq.demo2.util.PageDataResult;
import com.hzwq.demo2.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhaochunlin
 * @since 2020-08-13
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;
    @Autowired
    IPermissionService permissionService;

    @RequestMapping("/list")
    public ModelAndView toList() {
        ModelAndView mav = new ModelAndView("role/list");
        return mav;
    }

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult query(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit, String roleName,
                                String code) {
        PageDataResult pdr = new PageDataResult();
        QueryWrapper<Role> wrapper = new QueryWrapper<Role>();
        if (StringUtils.isNotBlank(roleName)) {
            wrapper.like("role_name", roleName);
        }
        if (StringUtils.isNotBlank(code)) {
            wrapper.like("code", code);
        }
        PageHelper.startPage(page, limit);
        List<Role> list = roleService.list(wrapper);
        PageInfo<Role> pageInfo = new PageInfo<Role>(list);
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pdr.setList(list);
        return pdr;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ModelAndView toEdit(Integer id) {
        ModelAndView mav = new ModelAndView("role/edit");
        Role role = new Role();
        if (id != null) {
            role = roleService.getById(id);
        }
        mav.addObject("role", role);
        mav.addObject("permissionList", permissionService.list());
        return mav;
    }

    @RequestMapping("/updatePermission")
    @ResponseBody
    public ModelAndView toupdatePermission(Integer id) {
        ModelAndView mav = new ModelAndView("role/per-assignment");
        Role role = new Role();
        if (id != null) {
            role = roleService.getById(id);
        }
        mav.addObject("role", role);
        mav.addObject("permissionList", permissionService.list());
        return mav;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Result<Role> save(Role role) {
        roleService.saveRoleAndPermission(role);
        return new Result<Role>("0", "保存成功");
    }

    @RequestMapping("/del")
    @ResponseBody
    public Result<Role> del(Integer id) {
        if (id != null) {
            //判断role下是否存在用户，存在，则不能删除
          int i= roleService.selectCountById(id);
          if (i>0){
              System.out.println(i);
              return new Result<Role>("0", "存在用户，不能删除");
          }else {
              //不存在，可以删除
              roleService.removeById(id);
          }
        }
        return new Result<Role>("0", "删除成功");
    }

    @RequestMapping("/detail")
    @ResponseBody
    public ModelAndView toDetail(Integer id) {
        ModelAndView mav = new ModelAndView("role/detail");
        Role role = new Role();
        if (id != null) {
            role = roleService.getById(id);
        }
        mav.addObject("role", role);
        return mav;
    }
}
